package com.bean.DBClasses;

import java.util.Random;

public class RandomSelect {
	//产生 [left,right)内count个不重复的随机数，注意开闭区间
	public static Integer[] getRandomSelect(int left,int right,int count){
		Integer[] result = getRandomSelect(right-left,count);
		for(int i = 0;i<result.length;i++){
			result[i] += left;
		}
		return result;
	}
	//产生 [0,range)内count个不重复的随机数，注意开闭区间
	public static Integer[] getRandomSelect(int range,int count){
		if(count>range)count = range;
		Integer[] result = new Integer[count];
		boolean[] haseSelect = new boolean[range];//haseSelect[x-left]代表x是否被选过
		Random random = new Random();
		for(int i = 0;i<range;i++){
			haseSelect[i] = false;
		}
		for(int i = 0;i<count;i++){
			int r = random.nextInt(range);//[0,range),注意区间的开闭
			while(haseSelect[r]){
				r++;
				if(r>=range){
					r = 0;
				}
			}
			result[i] = r;
			haseSelect[r] = true;
		}
		return result;
	}
}
